package com.madev.isellcrazyitems;

import java.text.DecimalFormat;
import java.util.ArrayList;

import com.madev.isellcrazyitems.entity.User;
import com.madev.sales.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;

public class Checkout extends Activity {
	private User customer = new User();
	private TextView confirmationTV;
	private ArrayList<Integer> posArr = new ArrayList<Integer>();
	private String[] nameArr;
	private double[] priceArr;
	private int[] qtyArr;
    private final DecimalFormat df = new DecimalFormat("#.00");
    private TableLayout confirmationTable;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkout_layout);
        
        Bundle bundle = getIntent().getExtras();
        nameArr = bundle.getStringArray("nameArr");
        priceArr = bundle.getDoubleArray("priceArr");
        qtyArr = bundle.getIntArray("qtyArr");

        customer = (User) bundle.getSerializable("customer");
        
        confirmationTable = (TableLayout) findViewById(R.id.confirmation_table);
        
        confirmationTV = (TextView) findViewById(R.id.confirmation_tv);
        confirmationTV.setText("Thank you " + customer.getUserFullName() + " for purchasing from our store. These are the items you have purchased: ");
        
        LayoutParams ww = new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        
        TextView totalPriceTV = new TextView(this), totalQtyTV = new TextView(this);
        double totalPrice = 0;
        int totalQty = 0;
        
        for (int i = 0; i < nameArr.length; i++){
        	TableRow tr = new TableRow(this);
    		TextView nameTV = new TextView(this), priceTV = new TextView(this), qtyTV = new TextView(this), totalTV = new TextView(this);
    		
    		nameTV.setLayoutParams(ww);
    		nameTV.setPadding(5, 5, 5, 5);
    		nameTV.setGravity(Gravity.CENTER);
    		priceTV.setLayoutParams(ww);
    		priceTV.setPadding(5, 5, 5, 5);
    		priceTV.setGravity(Gravity.CENTER);
    		qtyTV.setLayoutParams(ww);
    		qtyTV.setPadding(5, 5, 5, 5);
    		qtyTV.setGravity(Gravity.CENTER);
    		totalTV.setLayoutParams(ww);
    		totalTV.setPadding(5, 5, 5, 5);
    		totalTV.setGravity(Gravity.CENTER);
    		
    		nameTV.setText(nameArr[i]);
    		priceTV.setText("$" + df.format(priceArr[i]));
    		qtyTV.setText(String.valueOf(qtyArr[i]));
    		
    		double qtyPrice = priceArr[i] * Double.valueOf(qtyArr[i]);
    		totalTV.setText("$" + df.format(qtyPrice));
    		
    		totalPrice += qtyPrice;
    		totalQty += qtyArr[i];
    		
    		tr.addView(nameTV);
    		tr.addView(priceTV);
    		tr.addView(qtyTV);
    		tr.addView(totalTV);
    		
    		confirmationTable.addView(tr);
        }
        
        TableRow tr = new TableRow(this);
        
        totalQtyTV.setLayoutParams(ww);
        totalQtyTV.setLayoutParams(new TableRow.LayoutParams(2));
        totalQtyTV.setGravity(Gravity.CENTER);
        totalQtyTV.setText(String.valueOf(totalQty));
        
        totalPriceTV.setLayoutParams(ww);
        totalPriceTV.setLayoutParams(new TableRow.LayoutParams(3));
        totalPriceTV.setGravity(Gravity.CENTER);
        totalPriceTV.setText("$" + df.format(totalPrice));
                
        tr.addView(totalQtyTV);
        tr.addView(totalPriceTV);
        
        confirmationTable.addView(tr);
        
        TableRow byeTR = new TableRow(this);
        TextView tv = new TextView(this);
        
        LayoutParams tvParams = new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        tvParams.span = 4;
    	tv.setLayoutParams(tvParams);
    	tv.setPadding(5, 5, 5, 5);
    	tv.setGravity(Gravity.CENTER);
    	tv.setText("Have a nice day! \u263A");
    	
    	byeTR.addView(tv);
    	
    	TableRow temp = new TableRow(this);
    	TextView tempTV = new TextView(this);
    	tempTV.setLayoutParams(tvParams);
    	temp.addView(tempTV);
    	
    	confirmationTable.addView(temp);
    	confirmationTable.addView(byeTR);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.checkout, menu);
        return true;
    }
    
	@Override
	public boolean onOptionsItemSelected (MenuItem item){
		Bundle b = new Bundle();
		b.putSerializable("customer", customer);
		
		if (item.getItemId() == R.id.menu_purchase_more){
			Intent purchaseScreen = new Intent(this, Purchase.class);
	        startActivity(purchaseScreen);
	        finish();
		}
		return true;
	}
    
}
